<?php
namespace app\member\controller;
use app\AdminRest;
use app\member\info\PermissionMember;
use app\member\model\Goods;
use app\member\model\Member;
use app\member\model\RightsRelation;
use app\shop\controller\AdminSellingWater;
use longbingcore\wxcore\WxSetting;
use think\App;
use app\member\model\Member as model;
use app\member\model\Level;
use app\member\model\Growth;
use app\member\model\Integral;
use app\member\model\StoredOrder;
use think\facade\Db;


class AdminMember extends AdminRest
{

    protected $model;

    protected $level_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

        $this->level_model = new Level();

        $this->growth_model = new Growth();

        $this->integral_model = new Integral();

        $this->store_order_model = new StoredOrder();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 10:49
     * @功能说明:会员列表
     */

    public function memberList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $member_growth = $this->level_model->memberGrowth($this->_uniacid);

        $where1[] = ['a.growth','>=',$member_growth];

        $where1[] = ['a.over_time','not between',[0,time()]];;

        //创建时间搜索
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //等级搜索
        if(!empty($input['level'])){

            $growth_section = $this->level_model->getGrowth($input['level']);

            $start_growth   = $growth_section['start_growth'];

            $end_growth     = !empty($growth_section['end_growth'])?$growth_section['end_growth']-1:$growth_section['end_growth'];

            $dis[] = !empty($end_growth)?['a.growth','between',"$start_growth,$end_growth"]:['a.growth','>',"$start_growth"];

        }
        $where = [];
        //昵称搜索
        if(!empty($input['name'])){

            $where[] = ['b.nickName','like','%'.$input['name'].'%'];

            $where[] = ['b.id','=',$input['name']];

        }
        //储值查询
        if(!empty($input['start_price'])&&!empty($input['end_price'])){

            $start_price = $input['start_price'];

            $end_price   = $input['end_price'];

            $dis[]       = ['a.stored','between',"$start_price,$end_price"];

        }

        $data = $this->model->memberList($dis,$where1,$where,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 11:21
     * @功能说明:会员等级详情
     */
    public function memberInfo(){

        $input = $this->_param;

        $dis = [

            'a.id' => $input['id']
        ];
        //用户信息
        $data['user_info'] = $this->model->memberInfo($dis);
        //电话号码
        if(!empty($data['user_info'])){

            $data['user_info']['member_phone'] = Db::name('longbing_card_user_phone')->where(['user_id'=>$data['user_info']['user_id']])->value('phone');
        }
        //统计信息
        $data['form_list'] = $this->model->listForm($input['id']);

        $dis = [

            'user_id' => $data['user_info']['user_id'],

            'status'  => 2

        ];

        if($input['type']==1){
            //成长值记录
            $data['data_list'] = $this->growth_model->growthList($dis,$input['limit']);

        }elseif($input['type']==2){
            //积分记录
            $data['data_list'] = $this->integral_model->integralList($dis,$input['limit']);

        }else{
            //储值记录
            $data['data_list'] = $this->store_order_model->recordList($dis,$input['limit']);

        }

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2020-07-15 13:24
     * @功能说明:
     */
    public function statusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $member = $this->model->getMemberInfo($dis);

        $status = $member['status']==1?0:1;

        $data  = $this->model->memberUpdate($dis,['status'=>$status]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 10:14
     * @功能说明:会员权限
     */
    public function memberAuth(){

        $persisson = new PermissionMember($this->_uniacid);

        $auth = $persisson->pAuth();

        return $this->success($auth);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 15:41
     * @功能说明:修改成长值
     */
    public function updateGrowth(){

        $input = $this->_input;

        $member = $this->model->memberInfo(['a.id'=>$input['id']]);

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $member['user_id'],

            'controller' => $this->_user['admin_id'],

            'growth_add' => $input['growth'],

            'growth_before' => $member['growth'],

            'growth_after'  => $input['growth'],

            'status'     => 2,

            'type'       => 1,

            'controller_type' => 1
        ];
        //添加记录
        $this->growth_model->growthAdd($insert);

        $order = [

            'uniacid' => $this->_uniacid,

            'user_id' => $member['user_id'],

            'to_uid'  => 0
        ];
        //给用户修改成长值
        $res = $this->level_model->upUserRightsEnd(['growth'=>$input['growth']],$order);

        return $this->success($res);

    }

    /**
     * User: chenniang
     * Date: 2019-09-09 14:36
     * @return \think\Response
     * descption:佣金流水列表
     */
    public function waterList(){

        $input = $this->_param;

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.nickName','like',"%{$input['name']}%"];

            $where[] = ['g.phone','like',"%{$input['name']}%"];
        }

        if(!empty($input['thing_type'])){

            $dis[] = ['a.thing_type','=',$input['thing_type']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.thing_type','in',[1,2,3]];

        $water_model = new \app\shop\model\AdminSellingWater();

        $data = $water_model->storedWaterList($dis,$where,$this->_param['limit']);

        return $this->success($data);
    }










}
